package com.system.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.user.dao.LightingDao;
import com.system.user.dao.ThresholdDao;
import com.system.user.dao.WaterDao;
import com.system.user.entity.ThresholdEntity;
import com.system.user.entityVO.LightingVO;
import com.system.user.entityVO.WaterVO;
import com.system.user.service.ThresholdService;
import javafx.scene.effect.Lighting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("thresholdService")
public class ThresholdServiceImpl implements ThresholdService {

    @Autowired
    private ThresholdDao thresholdDao;

    @Autowired
    private WaterDao waterDao;

    @Autowired
    private LightingDao lightingDao;

    @Override
    public List<ThresholdEntity> selectByBNumber(Integer uId) {
        List<WaterVO> waterEntities = waterDao.selectByUId(uId, null);
        List<LightingVO> lightingVOS = lightingDao.selectByUId(uId, null);
        List<String> bNumbers = new ArrayList<>();
        for (WaterVO w : waterEntities) {
            bNumbers.add(w.getWaterNumber());
        }
        for (LightingVO l : lightingVOS) {
            bNumbers.add(l.getLightingNumber());
        }
        List<ThresholdEntity> thresholdEntities = thresholdDao.selectByBNumber(bNumbers);
        return thresholdEntities;
    }

    @Override
    public Double findMax(String bNumber) {
        return thresholdDao.selectMax(bNumber);
    }

    @Override
    public boolean add(String bNumber, Double max) {
        if (thresholdDao.insertThreshold(bNumber, max) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(ThresholdEntity threshold) {
        if (thresholdDao.updateThreshold(threshold) > 0) {
            return true;
        }
        return false;
    }
}
